﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LIM.controller;

namespace LIM.forms.uc_CRM_BaoGia_ChiTiet
{
    public partial class uc_CRM_BaoGia_ChiTiet_ThongTinBenNhan : UserControl
    {
        public uc_CRM_BaoGia_ChiTiet_ThongTinBenNhan()
        {
            InitializeComponent();

            VNThanhLoad();
        }
        //\\VNThanh
        void VNThanhLoad()
        {
            this.radMenuItem1.Click += new EventHandler(radMenuItem1_Click);
        }

        public int RowID { get; set; }
        public int RowIDMaKhachHang { get; set; }
        public Thanh_KhachHang Thanh_KhachHang { get; set; }
        public Thanh_BaoGia Thanh_BaoGia { get; set; }
        public int RowIDLienHe { get; set; }
        private LIM.controller.Thanh_CRMBaoGiaChiTietController _Controller;
        public LIM.controller.Thanh_CRMBaoGiaChiTietController Controller
        {
            get { if (_Controller == null) _Controller = new Thanh_CRMBaoGiaChiTietController(); return _Controller; }
            set { _Controller = value; }
        }

        // Lay gia tri cua cac control de luu.
        public Thanh_BaoGia GetControlValues()
        {
            Thanh_BaoGia result = new Thanh_BaoGia();
            result.RowID = RowID;
            result.RowIDKhachHang = RowIDMaKhachHang;
            result.TenKhachHang = this.radTxtTenKH.Text;
            result.DiaChi = this.radTxtDiaChi.Text;
            result.SoDienThoai = this.radTxtSoDienThoai.Text;
            result.SoFax = this.radTxtSoFax.Text;

            result.RowIDLienHe = RowIDLienHe;
            result.HoTenNguoiLienHe = this.radTxtNguoiNhan.Text;
            result.SoDiDong = this.radTxtSoDienThoaiLH.Text;
            result.Email = this.radRadioButton1.IsChecked ? this.radTxtEmailCaNhan.Text : this.radTxtEmailCongTy.Text;

            return result;
        }

        // Reset lai cac control lien quan ComboBox Khach Hang.
        public void SetControlValues(Thanh_KhachHang pThanh_KhachHang)
        {
            Thanh_KhachHang = pThanh_KhachHang;
            if (pThanh_KhachHang != null)
            {
                RowIDMaKhachHang = pThanh_KhachHang.RowID;
                this.radTxtTenKH.Text = pThanh_KhachHang.TenGoi;
                this.radTxtDiaChi.Text = pThanh_KhachHang.DiaChi;
                this.radTxtSoDienThoai.Text = pThanh_KhachHang.SoDienThoai;
                this.radTxtSoFax.Text = pThanh_KhachHang.SoFax;

                this.radListView1.DisplayMember = "HoTen";
                this.radListView1.ValueMember = "RowID";
                this.radListView1.DataSource = Controller.GetsLienHe(pThanh_KhachHang.RowID);
            }
            else
            {
                RowIDMaKhachHang = default(int);
                this.radTxtTenKH.Text =
                this.radTxtDiaChi.Text =
                this.radTxtSoDienThoai.Text =
                this.radTxtSoFax.Text = string.Empty;

                this.radListView1.DisplayMember = "HoTen";
                this.radListView1.ValueMember = "RowID";
                this.radListView1.DataSource = null;
            }
        }

        // Set cac control cua BaoGia khi open.
        public void SetControlValues(Thanh_BaoGia pThanh_BaoGia)
        {
            if (pThanh_BaoGia != null)
            {
                Thanh_BaoGia = pThanh_BaoGia;
                RowID = pThanh_BaoGia.RowID;
                RowIDMaKhachHang = pThanh_BaoGia.RowIDKhachHang;
                this.radTxtTenKH.Text = pThanh_BaoGia.TenKhachHang;
                this.radTxtDiaChi.Text = pThanh_BaoGia.DiaChi;
                this.radTxtSoDienThoai.Text = pThanh_BaoGia.SoDienThoai;
                this.radTxtSoFax.Text = pThanh_BaoGia.SoFax;

                this.radListView1.SelectedIndex = -1;
                foreach (var item in this.radListView1.Items)
                {
                    if (item.Value.ToString() == pThanh_BaoGia.RowIDLienHe.ToString())
                    {
                        this.radListView1.SelectedItem = item;
                        RowIDLienHe = pThanh_BaoGia.RowIDLienHe;
                        break;
                    }
                }
                this.radTxtNguoiNhan.Text = pThanh_BaoGia.HoTenNguoiLienHe;
                this.radTxtSoDienThoaiLH.Text = pThanh_BaoGia.SoDiDong;
                this.radRadioButton1.IsChecked = this.radTxtEmailCaNhan.Text == pThanh_BaoGia.Email;
                this.radRadioButton2.IsChecked = !this.radRadioButton1.IsChecked;
                //this.radTxtEmailCaNhan.Text = pThanh_BaoGia.Email;
                //this.radTxtEmailCongTy.Text = pThanh_BaoGia.Email;
            }
        }

        public void RefreshControl()
        {
            SetControlValues(Thanh_KhachHang);
            SetControlValues(Thanh_BaoGia);
        }
        // Refresh.
        void radMenuItem1_Click(object sender, EventArgs e)
        {
            RefreshControl();
        }

        // An hien danh sach nguoi nhan.
        private void radButton1_Click(object sender, EventArgs e)
        {
            this.radPanel1.Visible = !this.radPanel1.Visible;
        }

        // Khi chon lien he trong combobox.
        private void radListView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.radListView1.SelectedItem != null)
            {
                Thanh_LienHe oThanh_LienHe = this.radListView1.SelectedItem.DataBoundItem as Thanh_LienHe;
                if (oThanh_LienHe != null)
                {
                    RowIDLienHe = oThanh_LienHe.RowID;
                    radTxtNguoiNhan.Text = oThanh_LienHe.HoTen;
                    radTxtSoDienThoaiLH.Text = oThanh_LienHe.SoDiDong;
                    radTxtEmailCaNhan.Text = oThanh_LienHe.EmailCaNhan;
                    radTxtEmailCongTy.Text = oThanh_LienHe.EmailCongTy;
                }
            }
            this.radPanel1.Visible = false;
        }
    }
}
